package com.admin.boot.modules.system.controller;

import com.admin.boot.common.utils.QueryGenerator;
import com.admin.boot.modules.system.entity.SysMenu;
import com.admin.boot.modules.system.entity.SysMenuPermission;
import com.admin.boot.modules.system.service.ISysMenuPermissionService;
import com.admin.boot.modules.system.service.ISysMenuService;

import com.admin.boot.common.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version v3.0
 * @Description: TODO
 * @author: admin-boot
 * @date: 2024-02-03
 */
@Api(tags = "菜单表")
@RestController
@RequestMapping("/sysMenu/sysMenu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysMenuPermissionService sysMenuPermissionService;

    @GetMapping("/list")
    @ApiOperation(value = "菜单表-分页列表查询", notes = "菜单表-分页列表查询")
    public Result<?> list(SysMenu sysMenu,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<SysMenu> queryWrapper = QueryGenerator.initQueryWrapper(sysMenu);
        queryWrapper.lambda().eq(SysMenu::getPid, "0");
        Page<SysMenu> page = new Page<>(pageNo, pageSize);
        Page<SysMenu> sysMenuPage = sysMenuService.page(page, queryWrapper);

        for (SysMenu record : sysMenuPage.getRecords()) {
            LambdaQueryWrapper<SysMenu> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SysMenu::getPid, record.getId());
            List<SysMenu> list = sysMenuService.list(queryWrapper1);
            record.setChildren(list);
        }
        return Result.OK(sysMenuPage);
    }


    @GetMapping("/getMenuByRole")
    @ApiOperation(value = "菜单表-分页列表查询", notes = "菜单表-分页列表查询")
    public Result<?> getMenuByRole(String roleCode) {
        List<SysMenu> list = sysMenuService.list(null);
        List<SysMenu> collect = list.stream()
                .filter(m -> m.getPid().equals("0"))
                .peek(m -> m.setChildren(getChildrenMenu(list, m)))
                .collect(Collectors.toList());
        List<SysMenuPermission> permissions = sysMenuPermissionService.getByRole(roleCode);
        List<String> hasPermissionMenuIds = permissions.stream().map(SysMenuPermission::getMenuId).collect(Collectors.toList());
        HashMap<String, Object> result = new HashMap<>();
        result.put("menuList",collect);
        result.put("permissionList",hasPermissionMenuIds);
        return Result.OK(result);
    }
    List<SysMenu> getChildrenMenu(List<SysMenu> list, SysMenu sysMenu) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.stream()
                .filter(c -> c.getPid().equals(sysMenu.getId()))
                .peek(c -> c.setChildren(getChildrenMenu(list, c)))
                .collect(Collectors.toList());
    }


    @GetMapping("/queryRootList")
    @ApiOperation(value = "菜单表-分页列表查询", notes = "菜单表-分页列表查询")
    public Result<?> queryRootList(@RequestParam(value = "id", required = false) String id) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, 0);
        if (StringUtils.isNotBlank(id)) {
            queryWrapper.ne(SysMenu::getId, id);
        }
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        return Result.OK(list);
    }


    @GetMapping("/queryList")
    @ApiOperation(value = "菜单表-分页列表查询", notes = "菜单表-分页列表查询")
    public Result<?> queryList(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.list(null);
        return Result.OK(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "菜单表-添加", notes = "菜单表-添加")
    public Result<?> add(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.OK("添加成功！");
    }


    @PostMapping("/edit")
    @ApiOperation(value = "菜单表-编辑", notes = "菜单表-编辑")
    public Result<?> edit(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.OK("编辑成功！");
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "菜单表-删除", notes = "菜单表-删除")
    public Result<?> deleteById(String id) {
        sysMenuService.removeById(id);
        return Result.OK("删除成功！");
    }

    @GetMapping("/deleteBatch")
    @ApiOperation(value = "菜单表-批量删除", notes = "菜单表-批量删除")
    public Result<?> deleteBatch(String ids) {
        for (String id : ids.split(",")) {
            sysMenuService.removeById(id);
        }
        return Result.OK("删除成功！");
    }

}
